﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TorchView.Composition;
using TorchView.Game;
using System.Collections.ObjectModel;
using System.Windows;
using Microsoft.Practices.Unity;
using TorchView.Windows;
using System.Windows.Input;
using TorchView.SharedStashEditor.Models;
using System.Collections.Specialized;
using System.ComponentModel;
using TorchView.SharedStashEditor.Helpers;
using System.IO;
using TorchView.Store;

namespace TorchView.SharedStashEditor.ViewModels
{
    public class SharedStashViewModel : INotifyPropertyChanged, ISharedStashViewModel
    {
        private IUnityContainer _Container;
        private SharedStash _stash;
        private List<Item> _itemList;
        private ObservableCollection2<ItemSlot<ShareSlotType>> _itemSlot;
        private string _filename;

        public SharedStashViewModel(IView<ISharedStashViewModel> view, IUnityContainer container, SharedStash stash)
        {
            View = view;
            view.Model = this;

            _filename = Path.Combine(Torchlight.SaveDirectory, "sharedstash.bin");
            _Container = container;

            Inventory = new InventoryViewModel<ShareSlotType>(container);

            _SaveStashCommand = new DirectCommand(ExecuteSaveStash);
            _ReloadStashCommand = new DirectCommand(ExecuteReloadStash);

            ExecuteReloadStash(null);
        }

        private void ExecuteReloadStash(object parameter)
        {
            _stash = SharedStash.Load(_filename);
            _itemList = new List<Item>(_stash.Items);

            ItemSlot<ShareSlotType>[] items = new ItemSlot<ShareSlotType>[42]; // 6 rows X 7 cols
            foreach (Item item in _itemList)
            {
                items[(int)item.InventorySlot - 19] = new ItemSlot<ShareSlotType>((ShareSlotType)item.InventorySlot, item);
            }
            for (int i = 0; i < 42; ++i)
            {
                if (items[i] == null)
                    items[i] = new ItemSlot<ShareSlotType>((ShareSlotType)(i + 19));
            }

            _itemSlot = new ObservableCollection2<ItemSlot<ShareSlotType>>(items);
            Inventory.ItemSlot = _itemSlot;
            Inventory.ItemList = _itemList;

            NotifyPropertyChanged("Inventory");
        }

        private void ExecuteSaveStash(object parameter)
        {
            //Backup File
            LocalFileStore.Default.Store(_filename, "Shared Stash Editor Backup");

            foreach (ItemSlot<ShareSlotType> slot in _itemSlot)
            {
                if (slot.HasItem)
                    slot.Item.Save();
            }
            _stash.Items = _itemList.ToArray();
            _stash.Save(_filename);

            _Container.Resolve<IMessageBoxService>().Show(Application.Current.MainWindow, string.Format("\"{0}\" saved.", _filename), "Save", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        #region Commands
        private DirectCommand _SaveStashCommand;
        public ICommand SaveStashCommand { get { return _SaveStashCommand; } }

        private DirectCommand _ReloadStashCommand;
        public ICommand ReloadStashCommand { get { return _ReloadStashCommand; } }
        #endregion

        #region Properties
        public IView<ISharedStashViewModel> View { get; private set; }
        public InventoryViewModel<ShareSlotType> Inventory { get; private set; }
        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
